
#include "wxled.h"
#include <wx/bitmap.h>
#include "led_circle_black_16x16.xpm"

BEGIN_EVENT_TABLE( wxLed, wxStateCtrl )
	EVT_PAINT( wxLed::OnPaint )
END_EVENT_TABLE()

IMPLEMENT_DYNAMIC_CLASS( wxLedState, wxState )

wxLedState::wxLedState( wxStateId id, wxLedShape shape, wxLedColor color, bool on, bool blink, int blinkInterval )
:
wxState( id ),
m_shape( shape ),
m_color( color ),
m_on( on ),
m_blink( blink ),
m_blinkInterval( blinkInterval )
{

}

wxLed::wxLed( wxWindow* parent, wxWindowID id, wxPoint pos, wxSize size, int style )
:
wxStateCtrl( parent, id, pos, size, style ),
m_state( NULL )
{
	m_xpms[ wxLED_BLACK | wxLED_ROUND ] = led_circle_black_16x16_xpm;
}

bool wxLed::SetCurrentState( wxStateId id )
{
	wxState* state = GetState( id );
	wxASSERT_MSG( state, wxT("State pointer is NULL.") );
	wxLedState* newState = wxDynamicCast( state, wxLedState );
	if ( NULL != newState )
	{
		m_state = newState;
		return true;
	}
	else
	{
		return false;
	}
}

wxState* wxLed::GetCurrentState()
{
	return m_state;
}

void wxLed::OnPaint( wxPaintEvent& event )
{
	if ( NULL != m_state )
	{
		wxPaintDC dc(this);
		std::map< int, const char** >::iterator xpmIt = m_xpms.find( m_state->m_color | m_state->m_shape );
		wxBitmap bitmap( xpmIt->second );
		dc.DrawBitmap( bitmap, 0, 0, true );
	}
}

wxSize wxLed::DoGetBestSize() const
{
	return wxSize( 16, 16 );
}
